<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>使用正则表达式</title>
		<style type="text/css">
			body{margin: 0;}
		</style>
	</head>
	<body>
		<script src="../libs/minicomps.min.js"></script>
        <script src="../libs/Unify.js"></script>
        <script>
            const { Toggle, HSlider,VSlider,Panel, Canvas ,Button,TextArea,TextBox,TextInput,Label,Window,Image,LabelInput,TipsManager,RadioButton,ColorPicker,TabbarWindow,Checkbox,Knob,Stage,MoonTextArea,Dropdown} = mc
			const width = Stage.width;
			const height = Stage.height;
			const panel = new Panel(document.body, 0, 0, width,height);
            new Label(panel,100,10,'把内容粘贴到第一个输入框内')
            let [x,y,w,h]=[100,30,700,200];
            const input=new TextInput(panel,x,y);
            const ouput=new TextArea(panel,x,y+30);
            input.setSize(w);
            ouput.setSize(w,h);
            const numLabel=new Label(panel,x+w-100,y+h+35);
            y+=h+50;
            new Button(panel,x,y,'确定',onClick);
            new Button(panel,x+=100,y,'全选',onSelect1);
            new Button(panel,x+=100,y,'反选',onSelect2);
            x=100;
            let names=['数字','空格','括号','换行','小写字母','大写字母','拼音符号','所有符号','汉字','自定义的，以英文逗号为分割符，特殊符号前要加反斜杠','替换内容以英文逗号为分割符(如0,1把所有0转为1)[换行\\n]也可以输入其它符号分隔符'];
            let checkBoxs=[];
            let [input2,input3]=[];
            for(let i=0;i<names.length;i++){
                let checkBox=new Checkbox(panel, x, y+=30, '删除'+names[i], false);
                checkBoxs.push(checkBox);
                if(i==names.length-2){
                    input2=new TextInput(panel,x,y+=30);
                    input2.setSize(width-200);
                }else if(i==names.length-1){
                    input3=new TextInput(panel,x,y+=30);
                    input3.setSize(width-200);
                    checkBox.label=names[names.length-1];
                }
            }
            let input4=new TextInput(panel,x+480,y-35,',');
            input4.setSize(30)
            function onSelect1(e){
                for(let i=0;i<checkBoxs.length-1;i++){
                    let checkBox=checkBoxs[i];
                    checkBox.checked=true;
                }
            }
            function onSelect2(e){
                for(let i=0;i<checkBoxs.length-1;i++){
                    let checkBox=checkBoxs[i];
                    checkBox.checked=!checkBox.checked;
                }
            }
            function onClick(e){
                let str=input.text
                for(let i=0;i<checkBoxs.length;i++){
                    let checkBox=checkBoxs[i];
                    if(checkBox.checked){
                        switch(i){
                            case 0:str = str.replace(/[0-9]/g, '');                         break;         
                            case 1:str = str.replace(/[ ]/g, '');                           break;
                            case 2:str = str.replace(/[()（）\[ \]【】｛｝{}]/g, '');        break;
                            case 3:str = str.replace(/\n/g, '');                            break;
                            case 4:str = str.replace(/[a-z]/g, '');                         break;
                            case 5:str = str.replace(/[A-Z]/g, '');                         break;
                            case 6:str = str.replace(/[āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜüɑɡ]/g, ''); break;
                            case 7:str = str.replace(/[^\w^\s^\u4e00-\u9fa5]+/g, '');       break;
                            case 8:str = str.replace(/\p{sc=Han}/gu, '');                   break;
                            case 9:
                                var txts=input2.text.split(',');
                                for(let i=0;i<txts.length;i++){
                                    let reg=new RegExp(txts[i],'g');
                                    str = str.replace(reg, '');
                                }
                                break;
                            default:
                                    var txts=input3.text.split(input4.text);
                                    var [s1,s2]=txts;
                                    let reg=new RegExp(s1,'g');
                                    if(s2=='\\n'||s2=='<br>'){
                                        str = str.replace(reg,'\n');
                                    }else{
                                        str = str.replace(reg,s2);
                                    }
                                break;
                        }
                    }
                }
                ouput.text=str;
                numLabel.text='字数：'+str.length;
            }
        </script>
    </body>
</html>